﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QASampling.BusinessObject;

namespace QASampling.QAControl
{
    public partial class QALabelTextEdit : UserControl
    {
        private DataTable dataTable;
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        private string labelText;
        public string LabelText
        {
            get
            {
                return labelText;
            }
            set
            {
                labelText = value;
                NotifyPropertyChanged("LabelText");
            }
        }

        private string tableSource;
        public string TableSource
        {
            get
            {
                return tableSource;
            }
            set
            {
                tableSource = value;
                NotifyPropertyChanged("TableSource");
            }
        }

        private string fieldName;
        public string FieldName
        {
            get
            {
                return fieldName;
            }
            set
            {
                fieldName = value;
                NotifyPropertyChanged("FieldName");
            }
        }

        private int fieldLength;
        public int FieldLength
        {
            get
            {
                return fieldLength;
            }
            set
            {
                fieldLength = value;
                NotifyPropertyChanged("FieldLength");
            }
        }

        private JenisField fieldType;
        public JenisField FieldType
        {
            get
            {
                return fieldType;
            }
            set
            {
                fieldType = value;
                NotifyPropertyChanged("FieldType");
            }
        }

        public QALabelTextEdit()
        {
            InitializeComponent();
            this.PropertyChanged += OnPropertyChanged;
        }

        void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "LabelText")
            {
                label1.Text = labelText;
                qaEditButton1.FieldLabel = labelText;
            }
            else if (e.PropertyName == "TableSource")
            {
                qaEditButton1.TableSource = tableSource;
            }
            else if (e.PropertyName == "FieldName")
            {
                qaTextbox1.Name = fieldName;
                qaEditButton1.FieldName = fieldName;
            }
            else if (e.PropertyName == "FieldLength")
            {
                qaEditButton1.FieldLength = fieldLength;
            }
        }

        private void QALabelTextEdit_Load(object sender, EventArgs e)
        {
            try
            {
                this.dataTable = QAController.GetSPTWithField(tableSource, fieldName);
                this.qaTextbox1.DataBindings.Add("Text", dataTable, fieldName);
            }
            catch (Exception)
            {
                return;
            }
        }

        private void qaEditButton1_Click(object sender, EventArgs e)
        {
            try
            {
                this.dataTable = QAController.GetSPTWithField(tableSource, fieldName);
                this.qaTextbox1.DataBindings.Clear();
                this.qaTextbox1.DataBindings.Add("Text", dataTable, fieldName);
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
